# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Peter Budai <peterbudai@hotmail.com>

_realname=libgcrypt
pkgbase="mingw-w64-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.8.6
pkgrel=1
pkgdesc="General purpose cryptographic library based on the code from GnuPG (mingw-w64)"
arch=('any')
url="https://gnupg.org"
license=('LGPL')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs" "${MINGW_PACKAGE_PREFIX}-libgpg-error")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-libgpg-error") # "transfig" "${MINGW_PACKAGE_PREFIX}-ghostscript"
options=('staticlibs' 'strip')
source=("https://gnupg.org/ftp/gcrypt/libgcrypt/${_realname}-${pkgver}.tar.bz2"{,.sig}
        'libgcrypt-use-correct-def-file.patch'
        'Smarter-fig2dev-detection.all.patch')
# https://gnupg.org/signature_key.html
validpgpkeys=('D8692123C4065DEA5E0F3AB5249B39D24F25E3B6'
              '46CC730865BB5C78EBABADCF04376F3EE0856959'
              '031EC2536E580D8EA286A9F22071B08A33BD3F06'
              'D238EA65D64C67ED4C3073F28A861B1C7EFD60D9')
sha256sums=('0cba2700617b99fc33864a0c16b1fa7fdf9781d9ed3509f5d767178e5fd7b975'
            'SKIP'
            '55cf915badebb4b3cfa671eea725aebb77be791aa39a19caef090ded4f38d8eb'
            'd6855fc9b7a3a7fa440be1d9ae3477f8c08524d1aab1a8c7bb26d62b55382f72')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i "${srcdir}"/libgcrypt-use-correct-def-file.patch
  patch -p1 -i "${srcdir}"/Smarter-fig2dev-detection.all.patch

  autoreconf -fiv
}

build() {
  [[ -d "${srcdir}/build-${CARCH}" ]] && rm -rf "${srcdir}/build-${CARCH}"
  cp -rf "${srcdir}/${_realname}-${pkgver}" "${srcdir}/build-${CARCH}"
  cd "${srcdir}/build-${CARCH}"
  ./configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --enable-shared \
    --enable-static \
    --with-gpg-error-prefix=${MINGW_PREFIX}

  make
}

check() {
  cd "${srcdir}/build-${CARCH}"
  make check || true
}

package() {
  cd "${srcdir}/build-${CARCH}"
  make DESTDIR="${pkgdir}" install -j1
  find "${pkgdir}${MINGW_PREFIX}" -name '*.def' -o -name '*.exp' | xargs -rtl1 rm

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING.LIB" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING.LIB"
}
